package com.ruoyi.web.controller.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.constants.FrontConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;

/**
 * @author XuYanK
 * @date 2021/4/23 10:58
 */
@Slf4j
@Api(value = "获取redis存储的前端配置API", tags = {"获取redis存储的前端配置"})
@RestController()
@RequestMapping("/front")
public class FrontConstantsController {

    @Autowired
    private RedisCache redisCache;

    @Value("classpath:json/fireSquadron.json")
    private Resource fireSquadron;

    /**
     * 读取Json文件数据
     *
     * @param resource
     * @return
     *
     *
     */
    public JSONObject getJson(Resource resource) {
        try {
            String areaData = IOUtils.toString(resource.getInputStream(), StandardCharsets.UTF_8);
            return JSON.parseObject(areaData);
        } catch (IOException e) {
            log.error("", e);
        }
        return null;
    }

    /**
     * 项目初始加载
     * 无内容，加载项目已存储的json文件内容
     */
    @PostConstruct
    public void init() {
        Collection<String> keys = redisCache.keys(FrontConstants.FRONT_CONFIG_KEY + "*");
        //判断内容是否为空,为空则进行初始化
        if (keys.isEmpty()) {
            redisCache.setCacheObject(FrontConstants.FRONT_CONFIG_KEY + "1", FrontConstants.FRONT_CONFIG_VALUE_1);
            redisCache.setCacheObject(FrontConstants.FRONT_CONFIG_KEY + "2", FrontConstants.FRONT_CONFIG_VALUE_2);
            redisCache.setCacheObject(FrontConstants.FRONT_CONFIG_KEY + "3", getJson(fireSquadron));
            log.info("加载json文件：{}", fireSquadron);
            log.info("json文件内容：{}", getJson(fireSquadron));
        }
    }


    @ApiOperation("获取键的信息")
    @PostMapping("/getKey")
    public AjaxResult getKey(@RequestParam String key) {
        Object cacheObject = redisCache.getCacheObject(key);
        return AjaxResult.success(cacheObject);
    }

    @ApiOperation("获取键的列表")
    @GetMapping("/getKeyList")
    public AjaxResult getKeyList() {
        Collection<String> cacheObject = redisCache.keys(FrontConstants.FRONT_CONFIG_KEY + "*");
        return AjaxResult.success(cacheObject);
    }

    @ApiOperation("新增/修改键的信息")
    @PostMapping("/updateKey")
    public AjaxResult updateKey(@RequestParam String key,
                                @RequestParam String value) {
        //修改
        redisCache.setCacheObject(key, value);
        //查看
        Object cacheObject = redisCache.getCacheObject(key);
        return AjaxResult.success(cacheObject);
    }

}
